package com.cdd.order.service.impl;

import com.cdd.commonutils.orderVo.CourseWebVoOrder;
import com.cdd.commonutils.orderVo.MemberOrder;
import com.cdd.order.client.EduCourseClient;
import com.cdd.order.client.UcenterClient;
import com.cdd.order.entity.Order;
import com.cdd.order.mapper.OrderMapper;
import com.cdd.order.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdd.order.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;


/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author cdd
 * @since 2021-02-15
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduCourseClient eduCourseClient;

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public String createOrders(String courseId, String memberId) {

        Date date = new Date();
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();

        // 通过远程调用根据用户Id获取用户信息
        MemberOrder userInfoOrder = ucenterClient.getUserInfoOrder(memberId);
        // 通过远程调用根据课程Id获取课程信息
        CourseWebVoOrder courseInfoOrder = eduCourseClient.getCourseInfoOrder(courseId);

        //创建订单
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfoOrder.getTitle());
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName(courseInfoOrder.getTeacherName());
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(memberId);
        order.setEmail(userInfoOrder.getEmail());
        order.setNickname(userInfoOrder.getNickname());
        order.setStatus(0);
        order.setPayType(1);
        order.setGmtCreate(localDateTime);
        order.setGmtModified(localDateTime);
        baseMapper.insert(order);
        return order.getOrderNo();
    }
}
